<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>RabbitMQ介绍和场景使用 |  麦田麦穗</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-RabbitMQ介绍和场景使用"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  RabbitMQ介绍和场景使用
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/06/25/RabbitMQ%E4%BB%8B%E7%BB%8D%E5%92%8C%E5%9C%BA%E6%99%AF%E4%BD%BF%E7%94%A8/" class="article-date">
  <time datetime="2021-06-25T01:28:32.000Z" itemprop="datePublished">2021-06-25</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">11 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><pre class="language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3.1'</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>
    <span class="token key atrule">rabbitmq-manger</span><span class="token punctuation">:</span>
        <span class="token key atrule">image</span><span class="token punctuation">:</span> rabbitmq<span class="token punctuation">:</span>3<span class="token punctuation">-</span>management
        <span class="token key atrule">restart</span><span class="token punctuation">:</span> always
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> 5672<span class="token punctuation">:</span><span class="token number">5672</span>
          <span class="token punctuation">-</span> 8091<span class="token punctuation">:</span><span class="token number">15672</span></code></pre>

<p>使用docker进行快速安装</p>
<h2 id="历史"><a href="#历史" class="headerlink" title="历史"></a>历史</h2><ul>
<li>2007年8月，Rabbit公司发行了 RabbitMQ 1.1.0.</li>
<li>2009年8月，VMware出资4.2亿美元收购了SpringSource[9]，并在一段时间内作为VMware的一个独立的部门；公司原有的商业产品以vFabric应用套件名义发售。之后SpringSource又接连收购了RabbitMQ[10]、Redis[11]和Gemstone[12]。除Redis外，它们的产品也成为了vFabric应用套件的一部分。</li>
</ul>
<p>你会发现一个有趣而诧异的事实，VMware 和 Spring 、RabbitMQ、Redis这些大名鼎鼎的技术是同属一家公司。而 著名的 Spring背后的SpringSource公司主要以培训和咨询盈利。</p>
<span id="more"></span>

<h2 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h2><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210616101427.png" alt=""></p>
<blockquote>
<p>RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.</p>
</blockquote>
<p>这是官网最开头的一段介绍,这里将RabbitMQ类比为邮局派信场景，RabbitMQ扮演邮箱、邮局、邮差的角色。这里也看到了 <code>broker</code> 这个单词，Kafka也有broker这个概念，这里我们暂且将它理解为<code>代理</code>的意思。</p>
<h2 id="生产者-producer"><a href="#生产者-producer" class="headerlink" title="生产者 producer"></a>生产者 producer</h2><p>简单理解就是发送消息的程序</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618112249.png" alt=""></p>
<h2 id="消费者-consumer"><a href="#消费者-consumer" class="headerlink" title="消费者 consumer"></a>消费者 consumer</h2><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618112407.png" alt=""></p>
<h2 id="队列-queue"><a href="#队列-queue" class="headerlink" title="队列 queue"></a>队列 queue</h2><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618112446.png" alt=""></p>
<p>消息存储于queue当中，多个producer 可往一个 queue发送消息</p>
<h2 id="交换机-Exchange"><a href="#交换机-Exchange" class="headerlink" title="交换机 Exchange"></a>交换机 Exchange</h2><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618112856.png" alt=""></p>
<p>交换机是用来发送消息的AMQP实体。交换机拿到一个消息之后将它路由给一个或零个队列。它使用哪种路由算法是由交换机类型和被称作绑定（bindings）的规则所决定的(这个后面会讲到)。</p>
<p><strong>Exchange Type</strong></p>
<ul>
<li>direct  直连</li>
<li>fanout  扇出</li>
<li>topic   主题</li>
<li>headers  头</li>
</ul>
<p>除交换机类型外，在声明交换机时还可以附带许多其他的属性，其中最重要的几个分别是：</p>
<ul>
<li>Name</li>
<li>Durability （消息代理重启后，交换机是否还存在）</li>
<li>Auto-delete （当所有与之绑定的消息队列都完成了对此交换机的使用后，删掉它）</li>
<li>Arguments（依赖代理本身）</li>
</ul>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">exchange_declare</span><span class="token punctuation">(</span>
       <span class="token variable">$exchange</span><span class="token punctuation">,</span>
       <span class="token variable">$type</span><span class="token punctuation">,</span>
       <span class="token variable">$passive</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span>
       <span class="token variable">$durable</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span>
       <span class="token variable">$auto_delete</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
       <span class="token variable">$internal</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span>
       <span class="token variable">$nowait</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span>
       <span class="token variable">$arguments</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token variable">$ticket</span> <span class="token operator">=</span> <span class="token constant">null</span>
   <span class="token punctuation">)</span></code></pre>

<p>持久（durable）、暂存（transient）。持久化的交换机会在消息代理（broker）重启后依旧存在，而暂存的交换机则不会（它们需要在代理再次上线后重新被声明）。然而并不是所有的应用场景都需要持久化的交换机。</p>
<p><strong>默认交换机</strong></p>
<p>那就是每个新建队列（queue）都会自动绑定到默认交换机上，绑定的路由键（routing key）名称与队列名称相同。</p>
<p><strong>直连交换机 Direct</strong></p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618113517.png" alt=""></p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'hello_queue'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hello_exchange'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hello_key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<p>它是如何工作的：</p>
<ul>
<li>将一个队列绑定到某个交换机上，同时赋予该绑定一个路由键（routing key）</li>
<li>当一个携带着路由键为hello_key的消息被发送给直连交换机hello_exchange时，交换机会把它路由给hello_queue的队列。</li>
</ul>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hello_exchange'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hello_key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618113859.png" alt=""></p>
<p><strong>扇形交换机  fanout</strong></p>
<p>扇型交换机（funout exchange）将消息路由给绑定到它身上的所有队列，而不理会绑定的路由键。</p>
<p>如果N个队列绑定到某个扇型交换机上，当有消息发送给此扇型交换机时，交换机会将消息的拷贝分别发送给这所有的N个队列。</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618114009.png" alt=""></p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token comment">// RabbitFanoutProduce.php</span>
<span class="token variable">$faker</span> <span class="token operator">=</span> <span class="token class-name static-context">Factory</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'zh_CN'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建通道</span>
<span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">exchange_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.fanout'</span><span class="token punctuation">,</span> <span class="token class-name static-context">AMQPExchangeType</span><span class="token operator">::</span><span class="token constant">FANOUT</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$body</span> <span class="token operator">=</span> <span class="token variable">$faker</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$msg</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPMessage</span><span class="token punctuation">(</span><span class="token variable">$body</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'delivery_mode'</span> <span class="token operator">=></span> <span class="token class-name static-context">AMQPMessage</span><span class="token operator">::</span><span class="token constant">DELIVERY_MODE_PERSISTENT</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.fanout'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<p><code>RabbitFanoutProduce</code> 生产者定义了一个<code>test.fanout</code> 交换机类型是<code>AMQPExchangeType::FANOUT</code></p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token comment">// lmsConsoumer.php</span>
<span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建通道</span>
<span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.lms'</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.lms'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.fanout'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">echo</span> <span class="token string single-quoted-string">' [x] '</span><span class="token punctuation">,</span> <span class="token variable">$msg</span><span class="token operator">-></span><span class="token property">body</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_consume</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.lms'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span></code></pre>

<p><code>lmsConsoumer</code> 订阅了一个<code>test.lms</code>的队列并绑定到<code>test.fanout</code>交换机</p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token comment">// omsConsoumer.php</span>
<span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建通道</span>
<span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.oms'</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.oms'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.fanout'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">echo</span> <span class="token string single-quoted-string">' [x] '</span><span class="token punctuation">,</span> <span class="token variable">$msg</span><span class="token operator">-></span><span class="token property">body</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_consume</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.oms'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span></code></pre>

<p><code>omsConsoumer</code> 订阅了一个<code>test.oms</code>的队列并绑定到<code>test.fanout</code>交换机</p>
<p>示例中演示了，一个生产者生产消息给2个系统订阅消费。</p>
<p>Fanout 典型的一个应用场景是多个应用系统订阅一个消息事件。</p>
<p>eg： 客服系统、运营系统、财务系统同时订阅订单系统的订单状态变更事件。</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618114806.png" alt=""></p>
<p>这样不同系统都能各自使用各自的队列处理订单的变更业务逻辑。</p>
<p>细化下场景举个例子：</p>
<p>当订单完结 order close:</p>
<ul>
<li>客服系统需要给客户发送一个客户满意度调查</li>
<li>运营系统需要发送一张按订单消费金额配比的优惠券</li>
<li>财务系统需要计算这笔订单的入账</li>
</ul>
<p><strong>主题交换机 topic</strong></p>
<p>主题交换机（topic exchanges）通过对消息的路由键和队列到交换机的绑定模式之间的匹配，将消息路由给一个或多个队列。</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618114944.png" alt=""></p>
<p>topic有点类似 direct和fanout的结合体，即可以广播又可以按一定规则指定 route.</p>
<p>上面示例图案例：</p>
<ul>
<li>这是一个关于动物的消息消费程序</li>
<li>有关orange的动物将进入Q1 ，例如 fast.orange.pig 快速的橙色猪</li>
<li>有关 rabbit 和 lazy 的动物将进入 Q2 ，例如 lazy.green.rabbit 或 lazy.yellow.monkey</li>
<li>如果匹配多个模式则进入多个相应的队列，例如 lazy.orange.monkey 则会进入Q1和Q2</li>
</ul>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token variable">$faker</span> <span class="token operator">=</span> <span class="token class-name static-context">Factory</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'zh_CN'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建通道</span>
<span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">exchange_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token class-name static-context">AMQPExchangeType</span><span class="token operator">::</span><span class="token constant">TOPIC</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Q1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Q2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Q1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*.orange.*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Q2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*.*.rabbit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Q2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'lazy.#'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token variable">$body</span> <span class="token operator">=</span> <span class="token variable">$faker</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$msg</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPMessage</span><span class="token punctuation">(</span><span class="token variable">$body</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'delivery_mode'</span> <span class="token operator">=></span> <span class="token class-name static-context">AMQPMessage</span><span class="token operator">::</span><span class="token constant">DELIVERY_MODE_PERSISTENT</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'fast.orange.pig'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'lazy.green.rabbit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'test.topic'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'lazy.orange.monkey'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210618115131.png" alt=""></p>
<p>可以看到3个消息，发送到了2个队列。Q1和Q2各有2个，因为lazy.orange.monkey进入了2个队列。</p>
<p><strong>头交换机 header</strong></p>
<p>有时消息的路由操作会涉及到多个属性，此时使用消息头就比用路由键更容易表达，头交换机（headers exchange）就是为此而生的。头交换机使用多个消息属性来代替路由键建立路由规则。通过判断消息头的值能否与指定的绑定相匹配来确立路由规则。</p>
<p>它提供更多的匹配，可设置x-match 为any 或all 来控制是任意匹配还是全部匹配。</p>
<h3 id="队列-queue-1"><a href="#队列-queue-1" class="headerlink" title="队列 queue"></a>队列 queue</h3><p>AMQP中的队列（queue）跟其他消息队列或任务队列中的队列是很相似的：它们存储着即将被应用消费掉的消息。队列跟交换机共享某些属性，但是队列也有一些另外的属性。</p>
<ul>
<li>Name</li>
<li>Durable（消息代理重启后，队列依旧存在）</li>
<li>Exclusive（只被一个连接（connection）使用，而且当连接关闭后队列即被删除）</li>
<li>Auto-delete（当最后一个消费者退订后即被删除）</li>
<li>Arguments（一些消息代理用他来完成类似与TTL的某些额外功能）</li>
</ul>
<h3 id="消息确认"><a href="#消息确认" class="headerlink" title="消息确认"></a>消息确认</h3><p>消费者应用（Consumer applications） - 用来接受和处理消息的应用 - 在处理消息的时候偶尔会失败或者有时会直接崩溃掉。而且网络原因也有可能引起各种问题。这就给我们出了个难题，AMQP代理在什么时候删除消息才是正确的？AMQP 0-9-1 规范给我们两种建议：</p>
<ul>
<li>当消息代理（broker）将消息发送给应用后立即删除。（使用AMQP方法：basic.deliver或basic.get-ok）</li>
<li>待应用（application）发送一个确认回执（acknowledgement）后再删除消息。（使用AMQP方法：basic.ack）</li>
</ul>
<p>前者被称作自动确认模式（automatic acknowledgement model），后者被称作显式确认模式（explicit acknowledgement model)。</p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">echo</span> <span class="token string single-quoted-string">' [x] Received '</span><span class="token punctuation">,</span> <span class="token variable">$msg</span><span class="token operator">-></span><span class="token property">body</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span>
  <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token function">substr_count</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token operator">-></span><span class="token property">body</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'.'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">echo</span> <span class="token string double-quoted-string">" [x] Done\n"</span><span class="token punctuation">;</span>
  <span class="token variable">$msg</span><span class="token operator">-></span><span class="token function">ack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_consume</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'task_queue'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>

<p>以上代码示例展示ack使用，basic_consume的第4个参数要 设置为true。</p>
<h3 id="拒绝消息"><a href="#拒绝消息" class="headerlink" title="拒绝消息"></a>拒绝消息</h3><p>当一个消费者接收到某条消息后，处理过程有可能成功，有可能失败。应用可以向消息代理表明，本条消息由于“拒绝消息（Rejecting Messages）”的原因处理失败了（或者未能在此时完成）。当拒绝某条消息时，应用可以告诉消息代理如何处理这条消息——销毁它或者重新放入队列。当此队列只有一个消费者时，请确认不要由于拒绝消息并且选择了重新放入队列的行为而引起消息在同一个消费者身上无限循环的情况发生。</p>
<h3 id="连接"><a href="#连接" class="headerlink" title="连接"></a>连接</h3><p>AMQP连接通常是长连接。AMQP是一个使用TCP提供可靠投递的应用层协议。AMQP使用认证机制并且提供TLS（SSL）保护。当一个应用不再需要连接到AMQP代理的时候，需要优雅的释放掉AMQP连接，而不是直接将TCP连接关闭。</p>
<h3 id="虚拟主机-vhost"><a href="#虚拟主机-vhost" class="headerlink" title="虚拟主机 vhost"></a>虚拟主机 vhost</h3><p>为了在一个单独的代理上实现多个隔离的环境（用户、用户组、交换机、队列 等），AMQP提供了一个虚拟主机（virtual hosts - vhosts）的概念。这跟Web servers虚拟主机概念非常相似，这为AMQP实体提供了完全隔离的环境。</p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'test-vhost'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

<h2 id="动态匹配模型"><a href="#动态匹配模型" class="headerlink" title="动态匹配模型"></a>动态匹配模型</h2><p>生产者</p>
<pre class="language-php" data-language="php"><code class="language-php"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">execute</span><span class="token punctuation">(</span><span class="token class-name type-declaration">InputInterface</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">OutputInterface</span> <span class="token variable">$output</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">int</span>
   <span class="token punctuation">&#123;</span>
       <span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token comment">// 创建通道</span>
       <span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">exchange_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.lifecycle'</span><span class="token punctuation">,</span> <span class="token class-name static-context">AMQPExchangeType</span><span class="token operator">::</span><span class="token constant">TOPIC</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token comment">//定义一个默认 全状态队列</span>
       <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.all'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token comment">//匹配 全状态 订单消息</span>
       <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.all'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'order.lifecycle'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'order.*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token variable">$faker</span> <span class="token operator">=</span> <span class="token class-name static-context">Factory</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'zh_CN'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token variable">$orderStatus</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
           <span class="token keyword static-context">self</span><span class="token operator">::</span><span class="token constant">ORDER_CREATE</span><span class="token punctuation">,</span> <span class="token keyword static-context">self</span><span class="token operator">::</span><span class="token constant">ORDER_SENDED</span><span class="token punctuation">,</span> <span class="token keyword static-context">self</span><span class="token operator">::</span><span class="token constant">ORDER_WAIT_PAY</span>
       <span class="token punctuation">]</span><span class="token punctuation">;</span>
       <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token variable">$orderStatus</span><span class="token punctuation">[</span><span class="token function">array_rand</span><span class="token punctuation">(</span><span class="token variable">$orderStatus</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
       <span class="token variable">$body</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
           <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token variable">$status</span><span class="token punctuation">,</span>
           <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$faker</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span>
           <span class="token string single-quoted-string">'order_no'</span> <span class="token operator">=></span> <span class="token variable">$faker</span><span class="token operator">-></span><span class="token property">uuid</span>
       <span class="token punctuation">]</span><span class="token punctuation">;</span>
       <span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$body</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token variable">$msg</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPMessage</span><span class="token punctuation">(</span><span class="token function">json_encode</span><span class="token punctuation">(</span><span class="token variable">$body</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'delivery_mode'</span> <span class="token operator">=></span> <span class="token class-name static-context">AMQPMessage</span><span class="token operator">::</span><span class="token constant">DELIVERY_MODE_PERSISTENT</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token comment">//消息 动态 绑定 路由键</span>
       <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_publish</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'order.lifecycle'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'order.'</span><span class="token operator">.</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token keyword">return</span> <span class="token class-name static-context">Command</span><span class="token operator">::</span><span class="token constant">SUCCESS</span><span class="token punctuation">;</span>
   <span class="token punctuation">&#125;</span></code></pre>

<pre class="language-php" data-language="php"><code class="language-php">消费者
 <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handPay</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">&#123;</span>
        <span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AMQPStreamConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'192.168.106.179'</span><span class="token punctuation">,</span> <span class="token number">5672</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 创建通道</span>
        <span class="token variable">$channel</span> <span class="token operator">=</span> <span class="token variable">$connection</span><span class="token operator">-></span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//定义 h66 消费 pay 队列</span>
        <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_declare</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'h66.pay'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//将 WAIY_PAY 类型消息 绑定到该队列</span>
        <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">queue_bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'h66.pay'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'order.lifecycle'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'order.WAIT_PAY'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token variable">$body</span> <span class="token operator">=</span> <span class="token function">json_decode</span><span class="token punctuation">(</span><span class="token variable">$msg</span><span class="token operator">-></span><span class="token property">body</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"处理数据...<span class="token interpolation"><span class="token punctuation">&#123;</span><span class="token variable">$body</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span></span> <span class="token interpolation"><span class="token punctuation">&#123;</span><span class="token variable">$body</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'order_no'</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span></span>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
        <span class="token comment">// 消费 队列</span>
        <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">basic_consume</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'h66.pay'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 阻塞队列监听事件</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token variable">$channel</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span></code></pre>

<p>代码示例中 生成者根据订单状态动态生产相应路由键消息到交换机，消费者订阅自己所需的状态到自己的队列当中进行消费。</p>
<p>例如 <code>handPay</code> 消费者只订阅待支付类型。</p>
<h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><ol>
<li>PHP的api在往未定义的交换机生产消息时不会抛出异常</li>
</ol>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ol>
<li><a target="_blank" rel="noopener" href="https://rabbitmq.mr-ping.com/">https://rabbitmq.mr-ping.com/</a></li>
<li><a target="_blank" rel="noopener" href="https://www.rabbitmq.com/getstarted.html">https://www.rabbitmq.com/getstarted.html</a></li>
</ol>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          Donate
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://visonforcoding.xyz/2021/06/25/RabbitMQ%E4%BB%8B%E7%BB%8D%E5%92%8C%E5%9C%BA%E6%99%AF%E4%BD%BF%E7%94%A8/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%9E%B6%E6%9E%84/" rel="tag">架构</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/07/07/php/%E5%AD%A6%E4%B9%A0%E7%BC%96%E5%86%99php%E6%8B%93%E5%B1%95/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            学习编写php拓展
          
        </div>
      </a>
    
    
      <a href="/2021/06/02/mysql%E7%9A%84waitimeout/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">mysql的waitimeout</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> vison
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="麦田麦穗"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://shenyu-vip.lofter.com">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/null">Gallery</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>Buy me a cup of coffee~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpeg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wallet.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>